// pages/news/news.js
var app = getApp()
// const DB = wx.cloud.database().collection("freedom_data")
const db = wx.cloud.database()
let title = ""

// app.globalData.title = title

var util = require('../../utils/util.js');

Page({

  /**
   * 页面的初始数据
   */
  data: {
    "Category":"",
    "mid":"",
    "latitude":"",
    "longitude":"",
    "content":"",
    "nickname":"",
    "avatarUrl": "",
    "_openid": "",
    "date":"",
    "date_start":"",
    "date_end":"",
    "cut":"",
    "time":"",
    "number":"",
    "chooseImgs":[],
    "title":"",
    "userName":"",
    "huodongjianjie":"",
    "onloadtime":"",
    "timechuo":"",
    "zhuban":"",
    "baomingway":"",
    "huodongneirong":"",
    "array": ["志愿", "社团", "讲座", "自由"],
    "index": 0,
    "date_start": "",
    "date_end":"",
    "date_cut":"",
    "time_start":"",
    "time_end":"00:00",
    
     "hasUserInfo": false
  },
  userNameInput(e){
    console.log(e)
    this.setData({
      userName:e.detail.value

    })
      },
  
      zhubanInput(e){
        console.log(e)
        this.setData({
          zhuban:e.detail.value
    
        })
          },

          huodongInput(e){
            console.log(e)
            this.setData({
              huodong:e.detail.value
        
            })
              },

              baomingwayInput(e){
                console.log(e)
                this.setData({
                  baomingway:e.detail.value
            
                })
                  },
                 renshu(e){
                    console.log(e)
                    this.setData({
                     renshu:e.detail.value
                
                    })
                      },

                  huodongneirongInput(e){
                    console.log(e)
                    this.setData({
                      huodongneirong:e.detail.value
                
                    })
                      },
                      huodongjianjieInput(e){
                        console.log(e)
                        this.setData({
                          huodongjianjie:e.detail.value
                    
                        })
                          },

  PickerChange: function (e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      index: e.detail.value,
      mid: e.detail.value,

    })
  },

   handleTextInput(e){
        console.log(e)
    //     this.setData({
    //       title:e.detail.value
    //     })
          },
    
          cardJump: function (e) {
      var that = this
            console.log("点击事件的打印",e)
            getApp().globalData.title = e.currentTarget.dataset.title
   
    db.collection('all').get().then(res => {
      console.log("数据库获取的打印", res)
      console.log("数据库", res.data.length)
      // var all_data = [];
      // for (let i = 0; i < res.data.length; i++) {
      //   console.log("空", res.data)
      //   all_data.push(res.data[i])
      // }
      // console.log("ds", all_data)
      // that.setData({
      //   markers: all_data
      // })

      // var zhiyuan = [];
      // var shetuan = [];
      // var jiangzuo = [];
      // var ziyou = [];
      // for (let i = 0; i < res.data.length; i++) {
      //   console.log("空", res.data[i])
      //   if (res.data[i].mid == 0) {
      //     zhiyuan.push(res.data[i])
      //   };
      //   if (res.data[i].mid == 1) {
      //     shetuan.push(res.data[i])
      //   };
      //   if (res.data[i].mid == 2) {
      //     jiangzuo.push(res.data[i])
      //   };
      //   if (res.data[i].mid == 3) {
      //     ziyou.push(res.data[i])
      //   }
      // }
      that.setData({
     id:res.data.length+1
      })
    })

     


    wx.getStorage({
      
      key: 'markers',
      success (res) {
        wx.showToast({
          title: '保存成功请提交',
          duration:2000
        })
        //   that.setData({
        //         latitude:res.data[0].latitude,
        //   longitude:res.data[0].longitude,
        //   content:res.data[0].callout.title

        //       })

        console.log("坐标点信息",res.data[0].callout.content)
      }
    })
    wx.getStorage({
      key: 'userInfo',
      success (res) {
          that.setData({
               nickname:res.data.nickName,
               avatarUrl:res.data.avatarUrl

              })
        console.log("用户个人信息",res.data)
      }
    })
   
    wx.getStorage({
     
      key: 'openid',
      success (res) {
          that.setData({
              _openid:res.data
              })
        console.log("_openid",res.data)
      }
    })
    wx.setStorage({
      key:"informations",
      data:that.data

      

    })
    wx.getStorage({
      key: 'informations',
      success (res) {
          
        console.log("informations",res.data)
      }
    })

    that.setData({

      hasUserInfo: true
    })


           
    //         wx.navigateTo({
    //            url: '../../pages/end4/end4'
    //         })
          },

        upload: function (e) {
      var that=this
         db.collection('all').add({
        data:{
          "mid":that.data.mid,
          "Category":that.data.array[that.data.mid],
          "id":that.data.id,
          "_id": "",
          "clusterId": 1,              
          "joinCluster": true,
          "latitude": that.data.latitude,
          "longitude": that.data.longitude,
          "iconPath": "https://646f-dongyingyin-3khfy-1300530180.tcb.qcloud.la/dingdian.png?sign=8a288a66356343428164e98489926eb9&t=1620890661",
          "width": 30.0,
          "height": 30.0,             
              "callout": {
                "content": that.data.title,
                "time": that.data.date_start+"~"+ that.data.date_end+"("+that.data.time_start+"~"+ that.data.time_end+")",
                "address":that.data.huodong,
                "sponsor":that.data.zhuban,
                "padding": 10.0,
                "display": "ALWAYS",
                "textAlign": "center",
                "color": "#ffffff",
                "bgColor": "#ffe74b",
                "borderRadius": 10.0,                
            },
                                                                                                        
            "detail":{
              "mid":"",
              "image": that.data.imgurls,
              "small_image": that.data.imgurls,
              "title": that.data.title,
              "time":that.data.date_start+"~"+ that.data.date_end+"("+that.data.time_start+"~"+ that.data.time_end+")",
              "date_start": that.data.date_start,
              "date_end": that.data.date_end,
              "time_start": that.data.time_start,
              "time_end": that.data.time_end,
              "name":that.data.userName,           
         
            "text":that.data.huodongjianjie,
            "address":that.data.huodong,
            "sponsor":that.data.zhuban,
            "neirong":that.data.huodongneirong,
            "person":that.data.renshu,
            "register":that.data.baomingway,
            "cut":that.data.cut
        },
        "usermessage":{
               "openid":that.data._openid,
            "username":that.data.userName,
            "uploadtime":util.formatTime(new Date()),                
            "loaddate":new Date().getTime() 

  }
        },
        success: function(res) {
        

          console.log("上传成功".res);
          if(res){
            setTimeout(function(){
          wx.showToast({
            title: '上传成功',
            duration:2000
          })
        },100)
          
         }
         wx.navigateTo({
          url: '../../pages/end/end'
        })
        
        }

 
         
      })
             
          
            },

         returnJump: function (e) {
         
             
              wx.navigateTo({
                 url: '../../pages/end4/end4'
              })
            },

      handleremoveImg(e){
        const {index} = e.currentTarget.dataset;
        console.log(index);
        let {chooseImgs} = this.data   
        chooseImgs.splice(index,1);
        this.setData({
          chooseImgs
        })},

        bindDate_start: function (e) {
          console.log('picker发送选择改变，携带值为', e.detail.value)
          this.setData({
            date_start: e.detail.value
          })
        },

        bindDate_end: function (e) {
          console.log('picker发送选择改变，携带值为', e.detail.value)
          this.setData({
            date_end: e.detail.value
          })
        },

        time_start: function (e) {
          console.log('picker发送选择改变，携带值为', e.detail.value)
          this.setData({
            time_start: e.detail.value
          })
        },

        time_end: function (e) {
          console.log('picker发送选择改变，携带值为', e.detail.value)
          this.setData({
            time_end: e.detail.value
          })
        },

        cut: function (e) {
          console.log('picker发送选择改变，携带值为', e.detail.value)
          this.setData({
           cut: e.detail.value
          })
        },

        numberinput: function (e) {
          console.log('picker发送选择改变，携带值为', e.detail.value)
          this.setData({
          number: e.detail.value
          })
        },



        handleChooseImg(){
          let that=this;
          var that_ = this;
      
          console.log("点击了上传")
          wx.chooseImage({
            count: 1,
            sizeType: ['original', 'compressed'],
            sourceType: ['album', 'camera'],
            success:(res) => {
              
              const tempFilePaths = res.tempFilepaths;
              console.log("选择成功2",res);
               that_.setData({
                          src:res.tempFilePaths[0],
                          src_list:res.tempFilePaths,
                          judge:res.tempFilePaths["length"]
                          
                         }),
              
              that.setData({
                // chooseImgs:result.tempFilePaths
                chooseImgs:[...this.data.chooseImgs,...res.tempFilePaths],
      
      
                // chooseImgs:[...result.tempFilePaths]
              })
              //  that.pic_api(that.data.src_list[0]);
              console.log("读取成功1",res);
      
               var src_list=res.tempFilePaths
                  // var judge=res.tempFilePaths["length"]
                  console.log("src_list",src_list)
                  src_list.forEach((item,idx)=>{
                    var fileName=Date.now()+"_"+idx;
                    that.uploadImg(fileName,item)
                  })
      
      
      
          
              that.uploadImg(res.tempFilePaths[0]);
              // that.uploadImg();
      
        
          }
          })
      
        },
        
  uploadImg(fileName){
    
    wx.cloud.uploadFile({
        cloudPath: new Date().getTime()+'.png',
        filePath: fileName,
        
         success: (res)=>{
         console.log("上传成功",res)
         wx.showToast({
          title: '图片保存成功',
          duration:2000
        })
          
        //   if(res){
        //     setTimeout(function(){
        //   wx.showToast({
        //     title: '保存成功请提交',
        //     duration:2000
        //   })
        // },100)
          
        //  }
         this.setData({
          imgurls:res.fileID

         })
         console.log("成功",res.fileID)

        

              },
              fail: (res)=>{ 
                console.error    
                    },  
        }) 
    // that.uploadImg(res.tempFilePaths[0]);
    
    


    




  },

        

        





  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
   
    var TIME = util.formattime(new Date());

this.setData({
  time_start:TIME,

  time_end:TIME
});

var DATE = util.formatDate(new Date());



this.setData({

  date_end: DATE,
  date_start: DATE,
  cut:DATE

});

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this
    console.log()
    that.setData({
      title: getApp().globalData.title,
      latitude: getApp().globalData.curLatit,
      longitude:getApp().globalData.curLong

   
    })
    
    wx.getStorage({
      key: 'informations',
      success (res) {
        that.setData({
          title: getApp().globalData.title,

        })         
        console.log("informations登录",res.data)
      }
    })

    wx.getStorage({
      key: 'userInfo',
      success (res) {
        console.log("打印",res.data.nickname)
        that.setData({
          userName:res.data.nickname,

        })  
        console.log(res.data.nickname)
      }
    })

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})
